/*  XMLBuffer.java Version 101 - 3rd April 2008
 *  XMLBuffer.java fills a buffer full of XML for logging purpose. Reading it back is a task for you :-)
 *  See Versions for program version number
 *  Copyright (C) 2008  Andy Broomfield andy@newmediathinking.com
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
package net.blogbotplatform.blogbot;

import java.util.*;

public class XMLBuffer {
	long bufferStartTime;
	
	boolean xmlRunning = true;
	String xmlBuffer = "";
	
	String fileName;

	XMLBuffer() {
		startXMLBuffer();
	}

	void writeXML(String whichText, String filePath) { 
		// write xml data to the log file		
		closeXMLBuffer(whichText);
		
		System.out.println("writing XML file: " + fileName);
		Utils.writeTextFile(xmlBuffer, fileName, filePath);
	
		startXMLBuffer();
	}

	void startXMLBuffer() {
		Utils.setTimeStamp();
		bufferStartTime = System.currentTimeMillis();
		
		int s = Utils.second;
		int mi = Utils.minute;
		int h = Utils.hour;
		int d = Utils.day;
		int m = Utils.month;
		int y = Utils.year;

		fileName = "data" + "-" + y + "-" + m + "-" + d + "--" + h + "-" + mi + "-" + s + ".xml";

		xmlBuffer = "";
		xmlBuffer += "<xml>\n";
		xmlBuffer += "\t<date year=\"" + y + "\" month=\"" + m + "\" day=\"" + d + "\"/>\n";
		xmlBuffer += "\t<startime hour=\"" + h + "\" minute=\"" + mi + "\" second=\"" + s + "\"/>\n";

		xmlRunning = true;
	}

	void closeXMLBuffer(String whichText) {
		xmlRunning = false;
		xmlBuffer += "\t<blogtext>" + whichText + "</blogtext>\n";
		xmlBuffer += "</xml>\n";
	}

	void fillXML() {
		if (xmlRunning) {
			//println("protocol read\n");
			xmlBuffer += "\t<item>\n";
			xmlBuffer += "\t\t<elapsed milliSeconds=\"" + (System.currentTimeMillis() - bufferStartTime) + "\"/>\n";
			xmlBuffer += "\t\t<light value=\"" + BlogBotShop.dataBuffer[0] + "\"/>\n";
			/*for (int dataLoopCount = 0; dataLoopCount < 1; dataLoopCount++) {
				xmlBuffer += "\t\t<data-" + dataLoopCount + " value=\"" + BlogBotShop.dataBuffer[dataLoopCount] + "\"/>\n";
			}*/
			xmlBuffer += "\t</item>\n";
			//dataBuffer = {};
		}
	}

}
